package cn.milolab.djback.controller;

import cn.milolab.djback.annotation.DJRoleCheck;
import cn.milolab.djback.bean.entity.Employee;
import cn.milolab.djback.bean.entity.Job;
import cn.milolab.djback.bean.request.*;
import cn.milolab.djback.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 叶欣委
 * 活动控制层
 */
@RestController
@RequestMapping("/job")
public class JobController {
    @Autowired
    JobService jobService;

    @PostMapping
    @DJRoleCheck(Employee.RoleLevel.SECRETARY)
    public void addJob(@RequestBody @NonNull AddJobRequest addJobRequest) {
        jobService.addJob(addJobRequest);
    }

    @PutMapping
    @DJRoleCheck(Employee.RoleLevel.SECRETARY)
    public void updateJob(@RequestBody UpdateJobRequest updateJobRequest) {
        jobService.updateJob(updateJobRequest);
    }

    @DeleteMapping
    @DJRoleCheck(Employee.RoleLevel.SECRETARY)
    public void deleteJob(@RequestParam Integer id) {
        jobService.deleteJob(id);
    }

    @GetMapping
    @DJRoleCheck(Employee.RoleLevel.EMPLOYEE)
    public Map<String, Object> getJob(@RequestParam @Nullable Integer id, @RequestParam @NonNull String sort, @RequestParam @NonNull Integer page, @RequestParam @NonNull Integer limit) {
        return jobService.getJob(id, sort, page, limit);
    }

    @GetMapping("/mine")
    @DJRoleCheck(Employee.RoleLevel.EMPLOYEE)
    public List<Job> getMyJob( @NonNull @RequestAttribute Integer userId) {
        return jobService.getMyJob(userId);
    }

    @PostMapping("/apply")
    @DJRoleCheck(Employee.RoleLevel.EMPLOYEE)
    public void applyJob(@RequestBody ApplyJobRequest applyJobRequest,  @NonNull @RequestAttribute Integer userId) {
        jobService.applyJob(applyJobRequest, userId);
    }

    @GetMapping("/opening")
    @DJRoleCheck(Employee.RoleLevel.EMPLOYEE)
    public Map<String, Object> getOpeningJob() {
        return jobService.getOpeningJob();
    }

    @PostMapping("/exchange")
    @DJRoleCheck(Employee.RoleLevel.EMPLOYEE)
    public void exchangeJob(@RequestBody ExchangeJobRequest exchangeJobRequest) {
        jobService.exchangeJob(exchangeJobRequest);
    }

    @PostMapping("arrange")
    @DJRoleCheck(Employee.RoleLevel.SECRETARY)
    public void arrangeJob(@RequestBody ArrangeJobRequest arrangeJobRequest) {
        jobService.arrangeJob(arrangeJobRequest);
    }
}
